/* Recovers beliefs from GSU risk and beliefs data from the Raven 2019 and 2022 sessions */

* log file
capture log close _all
log using "Beliefs on Raven from 2019 and 2022.log", replace name(recover)

* configure Stata
capture: version 18
set processors 4
set more off
set scheme s1color
set seed 987654321
timer clear 1
timer on 1

* graphics font
graph set window fontface "Garamond"
graph set window fontface "Candara"

* tell us what version ran
about

* do all the files in the dta_dir? (yes or no)
global do_all "yes"

* do only the dta_file? if so, next list the name of the file, to get it done (yes or no)
global do_specific "no"

* name of specific input and output file, only relevant if $do_specific is "yes"
global use_specific   "raven_s32_q15"
global save_specific  "raven_s32_q15_saved"

* erase recovery_room files at the beginning and end?
global clean_room "no"

* create directories needed
capture: mkdir figures
capture: mkdir recovery_room
capture: mkdir recovered

* clean up
if "$clean_room" == "yes" {
	cd recovery_room
	local dfiles : dir "$dta_dir" files "*dta"
	foreach file of local dfiles {
		capture: erase "`file'"
	}
	cd ..
}

* load the code to recover beliefs
qui: do belief_recovery

* quiet, to speed up
qui {

* decide which risk model to use (eut or rdu)
local risk_model "rdu2019"

* collate the BHM estimates with beliefs
use bhm_`risk_model'_posterior
summ _loglikelihood if id==1
local mcmc = r(N)
rename _loglikelihood ll
drop if id==.
egen int sid = group(id)
merge m:1 id using link_raven_2019
drop _merge
generate int uid = LinkID
label variable uid "Unique ID linked to LinkID from DNH sessions"
save tmp_bhm_2019, replace
noi: di "Saved 2019 posterior with risk preferences ..."

* decide which risk model to use (eut or rdu)
local risk_model "rdu2022"

* collate the BHM estimates with beliefs
use bhm_`risk_model'_posterior
summ _loglikelihood if id==1
local mcmc = r(N)
rename _loglikelihood ll
drop if id==.
egen int sid = group(id)
merge m:1 id using link_raven_2022
drop _merge
generate int uid = (raven_session*100)+seat
label variable uid "Unique ID linked to raven_session and seat"
save tmp_bhm_2022, replace
noi: di "Saved 2022 posterior with risk preferences ..."

* restart with main data containing the beliefs data
use raven_beliefs_recovery, clear
drop if qid ==""
generate int uid = (raven_session*100)+seat
label variable uid "Unique ID linked to raven_session and seat"
summ uid LinkID
* no overlap with uid and LinkID, so this is safe
replace uid = LinkID if data2022 == 0
summ uid
local uid_min = r(min)
local uid_max = r(max)
tab uid

* extra variables to retain
local extra "raven_score LinkID uid data2022"
keep sid qid choice* nbin alpha beta `extra'
tab qid, missing
drop if qid == ""

* all questions had 80 tokens to allocate
generate int ntokens = 80
summ sid
local Nsub = r(max)

* QSR parameters specified as a global
foreach x in nbin ntokens alpha beta {
	summ `x'
	global `x' = r(mean)
}
di $nbin

* reports
forvalues x=1/8 {
	generate r_`x' = choice`x'
}
generate int question = .
forvalues q=1/36 {
	replace question = `q' if qid == "question`q'"
}
tab question, missing

* save data
keep sid question qid r_* `extra'
compress
sort sid qid
save tmp_sid, replace
noi: di "Saved beliefs data, and now generating recovery files for each subject ..."

* generate for each subject
summ question
local nq = r(max)
local ss = 0
forvalues s=`uid_min'/`uid_max' {
	forvalues q=1/`nq' {
		use tmp_sid, clear
		count if uid == `s'
		local nobs = r(N)
		if `nobs' > 0 {
			keep if question==`q' & uid==`s'
			save tmp_sid_p, replace
			summ data2022
			local y = r(mean)
			if `y' == 0 {
				local year = 2019
			}
			if `y' == 1 {
				local year = 2022
			}
			use tmp_bhm_`year', clear
			count if uid == `s'
			local nobs_bhm = r(N)
			if `nobs_bhm' > 0 {
				drop _log*
				keep if uid == `s'
				capture: generate eta = .
				capture: generate phi = .
				capture: generate mu = .
				sort sid
				merge m:1 uid using tmp_sid_p
				drop _merge
				generate long mcmc = `mcmc'
				compress
				if `q' == 1 {
					local ss = `ss'+1
				}
				save recovery_room/raven_s`ss'_q`q', replace
				des
				tab qid
				summ
			}
		}
	}
}

* recover beliefs
timer on 2

* CRRA, CARA, Power, and Expo-Power utility functions supported, the last one standing is the one used
global ufunc "cara"
global ufunc "expo"
global ufunc "power"
global ufunc "crra"

* Four probability weighting functions are supported, the last one standing in the next few lines is the one used if RDU is used
global pfunc "eut"
global pfunc "power"
global pfunc "inverse_s"
global pfunc "prelec2"

* controls the names of the parameters for the utility function
local upars "r"
if "$ufunc" == "expo" {
	local upars "r alpha"
}

* control the names of the parameters for the probability weighting function
local ppars ""
if "$pfunc" == "power" | "$pfunc" == "inverse_s" {
	local ppars "gamma"
}
if "$pfunc" == "prelec2" {
	local ppars "phi eta"
}


* do the belief recovery
noi: di "Recovering beliefs ..."

	if "$do_all" == "yes" {

		* subdirectory with data files
		cd recovery_room

		* use extended functions to get all the files in some data folder
		local dfiles : dir "$dta_dir" files "*dta"
		foreach file of local dfiles {

			* load the file
			use "`file'", clear

			* belief recovery
			beliefs_recovery `upars' `ppars'

			* save the output file in the current directory
			cd ..
			save "recovered/`file'", replace
			cd recovery_room

		}
		cd ..
	}

	if "$do_specific" == "yes" {

		* subdirectory with data files
		cd recovery_room

		use "$use_specific", clear

		* belief recovery
		beliefs_recovery `upars' `ppars'
		summ
		cd ..
		save "recovered/$save_specific", replace

	}


* clean up
if "$clean_room" == "yes" {
	noi: di "Cleaning up the recovery_room files ..."
	cd recovery_room
	local dfiles : dir "$dta_dir" files "*dta"
	foreach file of local dfiles {
		erase "`file'"
	}
	cd ..
}
capture: erase tmp.dta
capture: erase tmp_bhm_2019.dta
capture: erase tmp_bhm_2022.dta
capture: erase tmp_sid.dta
capture: erase tmp_sid_p.dta

* end of quietly
}

* time taken overall
timer off 2
timer list
local secs = r(t2)
local mins = `secs'/60
local hrs = `mins'/60
local secs_ = string(`secs', "%10.0f")
local mins_ = string(`mins', "%4.1f")
local hrs_ = string(`hrs', "%4.2f")
di "Belief recovery calculations by themselves took `secs_' seconds, `mins_' minutes, or `hrs_' hours."

* time taken overall
timer off 1
timer list
local secs = r(t1)
local mins = `secs'/60
local hrs = `mins'/60
local secs_ = string(`secs', "%10.0f")
local mins_ = string(`mins', "%4.1f")
local hrs_ = string(`hrs', "%4.2f")
di "Complete calculations took `secs_' seconds, `mins_' minutes, or `hrs_' hours."

log close recover
